Ti.include('../class/db.js');
Ti.include('../class/const.js');
Ti.include('../viewHelper/solucioDetailsViews.js');

var win = Ti.UI.currentWindow;

var objSolucio = Db.retrieveSolucio(win.idSolucio);
var votat = objSolucio.votat;
var textTitolSolucio = "#"+ win.numSolucio + ': ' +objSolucio.tt;
win.title = L("Idea") + " #" + objSolucio.id;
var txtVot = null;
if ( objSolucio.vt == 1 || objSolucio.vt == -1 ){
	txtVot = objSolucio.vt + "\n" + L("Vot");
}else{
	txtVot = objSolucio.vt + "\n" + L("Vots");
}

var views = solucioDetailsViews.getViews(textTitolSolucio,txtVot,objSolucio.ds);

if (objSolucio.vt > 0){
	views.votsLabel.backgroundColor="#11f605";
}else{
	if (objSolucio.vt < 0){
		views.votsLabel.backgroundColor="#f00900";
	}else{
		views.votsLabel.backgroundColor="#ffa327";
	}
}
views.titolSolucioView.add(views.votsLabel);
views.titolSolucioView.add(views.titolSolucioLabel);
win.add(views.titolSolucioView);

views.solucioTableView.top =views.titolSolucioView.height;
views.detailRow.add(views.descripcioSolucioLabel);
views.solucioTableView.setData([views.detailRow]);
win.add(views.solucioTableView);

Ti.App.addEventListener('setVoteButtonColor',function(votatObj){
	Ti.API.info("votat2:"+votatObj.votat + " status "+votatObj.status);
	if (votatObj.votat != 2){
		if(votatObj.votat == 1){
			upButton.votat = 1;
			upButton.backgroundColor='#11f605';
			equalButton.votat = 0;
			equalButton.backgroundColor = "#CCC";
			downButton.votat = 0;
			downButton.backgroundColor = "#CCC";
		}else if(votatObj.votat == 0){
			upButton.votat = 0;
			upButton.backgroundColor = "#CCC";
			equalButton.votat = 1;
			equalButton.backgroundColor = "#ffa327";
			downButton.votat = 0;
			downButton.backgroundColor = "#CCC";
		}else if(votatObj.votat == -1){
			upButton.votat = 0;
			upButton.backgroundColor = "#CCC";
			equalButton.votat = 0;
			equalButton.backgroundColor = "#CCC";
			downButton.votat = 1;
			downButton.backgroundColor = "#f00900";
		}	
	}
	
	if(votatObj.status == IDEA_STATUS_WAITING || votatObj.status == IDEA_STATUS_DONE){
		upButton.enabled = false;
		equalButton.enabled = false;
		downButton.enabled = false;
	}
	
	if(votatObj.status == IDEA_STATUS_WAITING || (votatObj.status == IDEA_STATUS_DONE && votatObj.votat == 2)){
		upButton.votat = 0;
		upButton.backgroundColor = "#CCC";
		equalButton.votat = 0;
		equalButton.backgroundColor = "#CCC";
		downButton.votat = 0;
		downButton.backgroundColor = "#CCC";
	}
});

if(Ti.App.user.hasToken()){
	views.activityIndicatorView.add(views.activityIndicator);
	win.add(views.activityIndicatorView);
	
	Ti.API.info("votat:"+votat);
	var viewButtons = Ti.UI.createView({
		height:45,
		bottom:0,
		layout:'horizontal'
	});
	
	var colorUp = null;
	if (objSolucio.votat == 1 || objSolucio.votat == 2){
		colorUp ='#11f605';
	}else{
		colorUp ='#CCC';
	}
	var upButton = Ti.UI.createButton({
		font:{size:"8"},
		left:5,
		height:40,
		width:97,
		borderRadius:12,
		color:'#000',
		backgroundColor:colorUp,
		style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN,
		title:L('Votar Si')
	});
	
	var colorEqual = null;
	if (objSolucio.votat == 0 || objSolucio.votat == 2){
		colorEqual ='#ffa327';
	}else{
		colorEqual ='#CCC';
	}
	var equalButton = Ti.UI.createButton({
		font:{size:"8"},
		left:8,
		height:40,
		width:97,
		borderRadius:12,
		color:'#000',
		backgroundColor:colorEqual,
		style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN,	
		title:L('En Blanc')
	});
	
	var colorDown = null;
	if (objSolucio.votat == 0 || objSolucio.votat == 2){
		colorDown ='#f00900';
	}else{
		colorDown ='#CCC';
	}
	var downButton = Ti.UI.createButton({
		font:{size:"8"},
		left:8,
		height:40,
		width:97,
		borderRadius:12,
		color:'#000',
		backgroundColor:colorDown,
		style:Titanium.UI.iPhone.SystemButtonStyle.PLAIN,	
		title:L('Votar No')
	});

	if(objSolucio.st == IDEA_STATUS_INCOURSE ){
		upButton.addEventListener('click',function(){
			if(upButton.votat !=1){
				votat = 1;
				views.activityIndicatorView.show();
				Ti.App.network_user.vote(win.idSolucio,votat);
			}
		});
		equalButton.addEventListener('click',function(){
			if(equalButton.votat !=1){
				votat = 0;
				views.activityIndicatorView.show();
				Ti.App.network_user.vote(win.idSolucio,votat);
			}
		});
		downButton.addEventListener('click',function(){
			if(downButton.votat != 1){
				votat = -1;
				views.activityIndicatorView.show();
				Ti.App.network_user.vote(win.idSolucio,votat);
			}
		});
	}
	views.solucioTableView.bottom=45;

	Ti.App.fireEvent('setVoteButtonColor',{votat:votat,status:objSolucio.st});
	win.barColor = '#333';
	viewButtons.add(upButton);
	viewButtons.add(equalButton);
	viewButtons.add(downButton);
	win.add(viewButtons);
}



Ti.App.addEventListener('voteKo', function(){
	Ti.App.network_user.showAlert(L('WARNING'),L("No s'ha pogut enviar el vot"));
	views.activityIndicatorView.hide();
});
Ti.App.addEventListener('voteOk', function(){
	views.activityIndicatorView.hide();
	var totalVots = Db.votarSolucio(win.idSolucio,votat);
	var totalVotsTxt = null;
	if ( totalVots == 1 || totalVots == -1 ){
		
		totalVotsTxt = totalVots + "\n" + L("Vot");
	}else{
		totalVotsTxt = totalVots + "\n" + L("Vots");
	}
	views.votsLabel.text = totalVotsTxt;
	
	if (totalVots > 0){
		views.votsLabel.backgroundColor="#11f605";
	}else{
		if (totalVots < 0){
			views.votsLabel.backgroundColor="#f00900";
		}else{
			views.votsLabel.backgroundColor="#ffa327";
		}
	}
	Ti.App.fireEvent('setVoteButtonColor',{votat:votat,status:objSolucio.st});
	Ti.App.fireEvent('updateSolucionsTableView');
});

